package com.tiancikeji.zaoke.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.KeyStroke;

import org.apache.commons.lang.StringUtils;
import org.javagui.swing.SwingUtils;

import com.tiancikeji.zaoke.gui.http.HttpLoader;
import com.tiancikeji.zaoke.gui.listener.PickFoodFormListener;
import com.tiancikeji.zaoke.gui.model.order.Order;

public class Form {

	private JFrame frame;

	private Order order;
	
	private PickFoodFormListener listener;
	
	private String token_state = HttpLoader.getState(Order.STATE_TOKEN + "");
	
	/**
	 * Create the application
	 */
	public Form(Order order, PickFoodFormListener listener, int index) {
		this.order = order;
		this.listener = listener;
		initialize(index);
	}

	/**
	 * Initialize the contents of the frame
	 */
	private void initialize(int index) {
		frame = new JFrame();
		frame.getContentPane().setLayout(null);
		frame.setResizable(false);
		frame.setSize(489, 316);
		SwingUtils.showCenter(frame);
		int step = index * 50;
		frame.setBounds(100 + step, 100 + step, 489, 316);
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frame.setTitle("订单详情 - " + + order.getOrderId());

		// main food
		final JLabel lblBno = new JLabel();
		lblBno.setText("主食");
		lblBno.setBounds(20, 23, 44, 15);
		frame.getContentPane().add(lblBno);

		JLabel txtBno = new JLabel();
		txtBno.setBounds(97, 19, 109, 21);
		txtBno.setText(order.getMfoodName());
		frame.getContentPane().add(txtBno);
		
		
		// drink food
		final JLabel lblDes = new JLabel();
		lblDes.setText("饮料");
		lblDes.setBounds(20, 53, 44, 15);
		frame.getContentPane().add(lblDes);

		JLabel txtBno2 = new JLabel();
		txtBno2.setBounds(97, 50, 109, 21);
		txtBno2.setText(order.getSfoodName());
		frame.getContentPane().add(txtBno2);
		
		// price
		final JLabel lblDes2 = new JLabel();
		lblDes2.setText("价格");
		lblDes2.setBounds(20, 87, 44, 15);
		frame.getContentPane().add(lblDes2);

		JLabel txtBno3 = new JLabel();
		txtBno3.setBounds(97, 84, 109, 21);
		txtBno3.setText(order.getSalePrice() + "");
		frame.getContentPane().add(txtBno3);
		
		// pay mode
		final JLabel lblDes3 = new JLabel();
		lblDes3.setText("支付方式");
		lblDes3.setBounds(20, 121, 88, 15);
		frame.getContentPane().add(lblDes3);

		JLabel txtBno4 = new JLabel();
		txtBno4.setBounds(97, 118, 109, 21);
		txtBno4.setText(order.getPayWay());
		frame.getContentPane().add(txtBno4);

		// status
		final JLabel lblDes4 = new JLabel();
		lblDes4.setText("订单状态");
		lblDes4.setBounds(20, 155, 88, 15);
		frame.getContentPane().add(lblDes4);

		JLabel txtBno5 = new JLabel();
		txtBno5.setBounds(97, 152, 109, 21);
		txtBno5.setText(order.getState());
		frame.getContentPane().add(txtBno5);
		
		// userid, username
		final JLabel lblDes5 = new JLabel();
		lblDes5.setText("用户号码");
		lblDes5.setBounds(270, 23, 88, 15);
		frame.getContentPane().add(lblDes5);

		JLabel txtBno6 = new JLabel();
		txtBno6.setBounds(347, 19, 129, 21);
		txtBno6.setText(order.getUserId() + "");
		frame.getContentPane().add(txtBno6);
		
		final JLabel lblDes6 = new JLabel();
		lblDes6.setText("用户名");
		lblDes6.setBounds(270, 53, 88, 15);
		frame.getContentPane().add(lblDes6);

		JLabel txtBno7 = new JLabel();
		txtBno7.setBounds(347, 50, 129, 21);
		txtBno7.setText(order.getUserName());
		frame.getContentPane().add(txtBno7);
		
		// card id
		final JLabel lblDes7 = new JLabel();
		lblDes7.setText("会员卡号");
		lblDes7.setBounds(270, 87, 88, 15);
		frame.getContentPane().add(lblDes7);

		JLabel txtBno8 = new JLabel();
		txtBno8.setBounds(347, 83, 129, 21);
		txtBno8.setText(order.getCardId() + "");
		frame.getContentPane().add(txtBno8);
		
		// pick time, order time
		final JLabel lblDes8 = new JLabel();
		lblDes8.setText("订餐时间");
		lblDes8.setBounds(270, 121, 88, 15);
		frame.getContentPane().add(lblDes8);

		JLabel txtBno9 = new JLabel();
		txtBno9.setBounds(347, 118, 129, 21);
		txtBno9.setText(order.getOrderTime());
		frame.getContentPane().add(txtBno9);

		final JLabel lblDes9 = new JLabel();
		lblDes9.setText("取餐时间");
		lblDes9.setBounds(270, 155, 88, 15);
		frame.getContentPane().add(lblDes9);

		JLabel txtBno10 = new JLabel();
		txtBno10.setBounds(347, 152, 129, 21);
		txtBno10.setText(order.getBookTime());
		frame.getContentPane().add(txtBno10);
		
		// pick
		final JButton btnPick = new JButton();
		btnPick.addActionListener(listener);
		btnPick.setBounds(270, 249, 67, 22);
		btnPick.setText("取餐");
		frame.getContentPane().add(btnPick);
		
		btnPick.setFocusable(true); 
		btnPick.registerKeyboardAction(btnPick.getActionForKeyStroke(KeyStroke
                .getKeyStroke(KeyEvent.VK_SPACE, 0, false)),
                KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false),
                JComponent.WHEN_FOCUSED);
		btnPick.registerKeyboardAction(btnPick.getActionForKeyStroke(KeyStroke
                .getKeyStroke(KeyEvent.VK_SPACE, 0, true)),
                KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true),
                JComponent.WHEN_FOCUSED);
		
		if(!StringUtils.equalsIgnoreCase(token_state, order.getState()) || StringUtils.equals(System.getProperty("env"), "http"))
			btnPick.setEnabled(true);
		else
			btnPick.setEnabled(false);
		
		// exit
		final JButton btnExit = new JButton();
		btnExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				frame.dispose();
			}
		});
		btnExit.setBounds(394, 249, 67, 22);
		btnExit.setText("退出");
		frame.getContentPane().add(btnExit);

	}

	public JFrame getFrame() {
		return frame;
	}

	public void setFrame(JFrame frame) {
		this.frame = frame;
	}

	public Order getOrder() {
		return order;
	}

	public void setOrder(Order order) {
		this.order = order;
	}

	public PickFoodFormListener getListener() {
		return listener;
	}

	public void setListener(PickFoodFormListener listener) {
		this.listener = listener;
	}
}
